package sk.jp.commons.utils.xml;

import org.xml.sax.SAXException;

/**
 * Throwed when validation of XML document failed.
 * @see SAXException
 * @author Jan Pastor 
 */
public class ValidationException extends SAXException {

    /**
     * Severity of exception.
     */
    public static final int FATAL_ERROR = 1;
    public static final int ERROR = 2;
    public static final int WARNING = 3;

    //----------------------------------------------------------------------

    private int severity;
    private Throwable cause;

    //----------------------------------------------------------------------

    /**
     * @see ValidationException(int, String, Exception)
     * @param severity
     * @param message
     */
    public ValidationException(int severity, String message) {
        super(message);
        this.severity = severity;
        cause = null;
    }

    /**
     * @see ValidationException(int, String, Exception)
     * @param severity
     * @param cause
     */
    public ValidationException(int severity, Exception cause) {
        super(cause);
        this.severity = severity;
        this.cause = cause;
    }

    /**
     * Creates instance.
     * @param severity severity of exception, see constants.
     * @param message
     * @param cause
     */
    public ValidationException(int severity, String message, Exception cause) {
        super(message, cause);
        this.severity = severity;
        this.cause = cause;
    }

    //----------------------------------------------------------------------

    /**
     * Return exception message.
     * @see super.getMessage()
     * @return exception message.
     */
    public String getMessage() {
        String s;
        switch(severity){
            case ValidationException.FATAL_ERROR: s = "(fatal error)"; break;
            case ValidationException.ERROR: s = "(error)"; break;
            case ValidationException.WARNING: s = "(warning)"; break;
            default: s = "";
        }
        return s + " " + super.getMessage();
    }

    /**
     * Message plus cause exception message.
     * @see this.getMessage
     * @return message plus cause exception message.
     */
    public String getMessageWithCause(){
        if(cause != null){
            return getMessage() + " (Cause: " + cause.toString() + ")";
        }else{
            return getMessage();
        }
    }

    /**
     * Returns cause.
     * @return cause.
     */
    public Throwable getCause() {
        return cause;
    }

    /**
     * Return exception severity. 
     * @return exception severity.
     */
    public int getSeverity() {
        return severity;
    }

}
